package cn.gz.demo.ui;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.adapter.LeftAdapter;
import cn.gz.demo.adapter.RightAdapter;
import cn.gz.demo.bean.Product;
import cn.gz.demo.databinding.SharesListBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.DataUtil;

/**
 * @author Liuyz
 * @date 2020/6/1015:04
 */
public class ShareListActivity extends BaseActivity {
    private SharesListBinding binding;
    private LeftAdapter leftAdapter = null;
    private RightAdapter rightAdapter = null;
    List<Product> leftList = new ArrayList<>();
    List<Product> rightList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_shares_list);

        //相互引用
        binding.right.rightTitleHorscrollView.setmSyncView(binding.rightContentHorscrollView);
        binding.rightContentHorscrollView.setmSyncView(binding.right.rightTitleHorscrollView);

        //setadapter
        binding.contentListViewLeft.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, true));
        leftAdapter = new LeftAdapter(context, leftList);
        binding.contentListViewLeft.setAdapter(leftAdapter);

        binding.contentListViewRight.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, true));
        rightAdapter = new RightAdapter(context, rightList);
        binding.contentListViewRight.setAdapter(rightAdapter);

        //get data
        new Thread() {
            @Override
            public void run() {
                super.run();

                List<Product> productList = DataUtil.getData(context);
                Message message = handler.obtainMessage();
                message.what = 0;
                message.obj = productList;
                message.sendToTarget();
            }
        }.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    List<Product> allList = (List<Product>) msg.obj;
                    leftList.clear();
                    rightList.clear();
                    leftList.addAll(allList);
                    rightList.addAll(allList);
                    leftAdapter.notifyDataSetChanged();
                    rightAdapter.notifyDataSetChanged();

                    break;
            }

        }
    };
}
